//
//  WKSettingViewController.m
//  WKDemo
//
//  Created by wangzhaohui-Mac on 14-8-14.
//  Copyright (c) 2014年 com.app. All rights reserved.
//

#import "WKSettingViewController.h"
#import "WKCommonGroup.h"
#import "WKCommonArrowItem.h"
#import "WKCommonSwitchItem.h"
#import "WKCommonCell.h"

@interface WKSettingViewController ()
@property (nonatomic,strong) NSMutableArray *groups;
@end

@implementation WKSettingViewController
/**
 *  用一个模型来描述每组的信息：组头、组尾、每组的所有行模型
 *  用一个模型来描述每行的信息：图标、标题、子标题、右边的样式（箭头、文字、数字、开关、打钩）
 *
 */
- (NSMutableArray *)groups
{
    if (_groups == nil) {
        _groups = [NSMutableArray array];
    }
    return _groups;
}

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    self.tableView.tableFooterView.width = 300;
    self.tableView.tableFooterView.x = 10;

}

/**
 *  拦截初始化，改变tableViewCell的样式
 */
- (id)init
{
    return [self initWithStyle:UITableViewStyleGrouped];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"设置";
    //设置tableView的内边距
    self.tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0);
    //取消tableView的分割线
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //初始化模型数据
    [self setupGroups];
    
    //设置退出按钮
    [self setupLogout];

}

/**
 *  初始化模型数据
 */
- (void)setupGroups
{
    [self setupGroup0];
    [self setupGroup1];
    
}
- (void)setupGroup0
{
    WKCommonGroup *group = [WKCommonGroup group];
    group.header = @"功能设置";
    [self.groups addObject:group];
    WKCommonSwitchItem *item0 = [WKCommonSwitchItem itemWithTitel:@"字体大小"];
    WKCommonSwitchItem *item1 = [WKCommonSwitchItem itemWithTitel:@"主题"];
    WKCommonSwitchItem *item2 = [WKCommonSwitchItem itemWithTitel:@"收藏时转发"];
    WKCommonSwitchItem *item3 = [WKCommonSwitchItem itemWithTitel:@"保存浏览进度"];
    WKCommonSwitchItem *item4 = [WKCommonSwitchItem itemWithTitel:@"转发时收藏"];
    WKCommonArrowItem *item5 = [WKCommonArrowItem itemWithTitel:@"通知设置"];
    group.items = @[item0,item1,item2,item3,item4,item5];
    
}

- (void)setupGroup1
{
    WKCommonGroup *group = [WKCommonGroup group];
    group.header = @"更多设置";
    [self.groups addObject:group];
    WKCommonArrowItem *item5 = [WKCommonArrowItem itemWithTitel:@"清除缓存(300M)"];
    WKCommonArrowItem *item0 = [WKCommonArrowItem itemWithTitel:@"推荐给朋友"];
    WKCommonArrowItem *item1 = [WKCommonArrowItem itemWithTitel:@"意见反馈"];
    WKCommonArrowItem *item2 = [WKCommonArrowItem itemWithTitel:@"帮助"];
    WKCommonArrowItem *item3 = [WKCommonArrowItem itemWithTitel:@"当前版本:3.9.6"];
    WKCommonArrowItem *item4 = [WKCommonArrowItem itemWithTitel:@"关于我们"];
    group.items = @[item5,item0,item1,item2,item3,item4];

}


/**
 *  设置退出按钮
 */
- (void)setupLogout
{
    UIButton *logout = [[UIButton alloc] init];
    logout.titleLabel.font = [UIFont boldSystemFontOfSize:14];
    logout.backgroundColor = [UIColor grayColor];
    [logout setTitle:@"退出当前账号" forState:UIControlStateNormal];
    [logout setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    logout.backgroundColor = [UIColor redColor];
    
    //设置退出按钮的尺寸
    logout.height = 35;
    
    self.tableView.tableFooterView = logout;
    
    
}


#pragma mark - Table view data source
/**组数*/
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    return self.groups.count;
}
/**每一组的行数*/
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    WKCommonGroup *group = self.groups[section];
    
    return group.items.count;
}

/**每一行显示的内容*/
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    WKCommonCell *cell = [WKCommonCell cellWithTableView:tableView];
    WKCommonGroup *group = self.groups[indexPath.section];
    cell.item = group.items[indexPath.row];
    // 设置cell所处的行号 和 所处组的总行数
    [cell setIndexPath:indexPath rowInSection:group.items.count];
    
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    WKCommonGroup *group = self.groups[section];
    return group.header;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //取出对应组的模型
    WKCommonGroup *group = self.groups[indexPath.section];
    //取出对应行的模型
    WKCommonItem *item = group.items[indexPath.row];
    
    //如果有要跳转的控制器
    if (item.destVcClass) {
        UIViewController *vc = [[item.destVcClass alloc] init];
        vc.title = item.title;
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    //如果有要执行的操作
    if (item.operation) {
        item.operation();
    }
    
}
@end
